package UserSum;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;

public class DriTest {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\MP\\json\\out\\out_user");
        if (file.exists()) {
            delFile(file);
            driver();
        } else {
            driver();
        }
    }

    public static void delFile(File file) {
        File[] files = file.listFiles();
        if (files != null && files.length != 0) {
            for (int i = 0; i < files.length; i++) {
                delFile(files[i]);
            }
        }
        file.delete();
    }

    public static void driver() throws Exception {
        Configuration conf = new Configuration();
//        conf.set("fs.default","hdfs://192.168.0.155:9000");
        Job job = Job.getInstance(conf);

        job.setJarByClass(DriTest.class);
        job.setMapperClass(MapTest.class);
        job.setReducerClass(RedTest.class);

        job.setMapOutputKeyClass(User.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(User.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job, "D:\\MP\\json\\User.json");
        FileOutputFormat.setOutputPath(job, new Path("D:\\MP\\json\\out\\out_user"));
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}
